import { getToken, removeToken, getCookie, setCookie, removeCookie, setToken } from '@/utils/cookie'

const state = {
    token: getToken(),
    isLogin: getCookie("isLogin"),
    currentUser: getCookie("currentUser") ? JSON.parse(getCookie("currentUser")!) : undefined
}

const mutations = {
    setToken(state: any, token: string) {
        state.token = token
        setToken(token, 1)
    },
    setIsLogin(state: any, isLogin: any) {
        state.isLogin = isLogin
        setCookie("isLogin", isLogin, 1)
    },
    setCurrentUser(state: any, user: any) {
        state.currentUser = user;
        setCookie("currentUser", JSON.stringify(user), 1)
    },
    removeCookie(state: any, key: string) {
        if (key === "token") {
            state.token = undefined
            removeToken()
        } else if (key === "isLogin") {
            state.isLogin = undefined
            removeCookie("isLogin")
        } else if (key === "currentUser") {
            state.currentUser = undefined
            removeCookie("currentUser")
        }
    }
}

const actions = {
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}